<!--试题管理-->
<template>
  <div>
    <el-form :inline="true" :model="requestData" style="width: 100%;margin-bottom: -10px;margin-top: -10px;" label-width="80px">
      <el-form-item v-if="$route.query.type == 'add'">
        <el-button size="mini" icon="el-icon-back" type="success" @click="returnRoom">返回考场</el-button>
      </el-form-item>
      <el-form-item label="考卷标题">
        <el-input size="mini" v-model="requestData.map.ep_name"></el-input>
      </el-form-item>
      <el-form-item label="科目">
        <div class="block">
          <el-cascader
            placeholder="请选择"
            :options="options"
            filterable
            size="mini"
            v-model="DataForm.f_sb_id"
          ></el-cascader>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" icon="el-icon-search" @click="searchInfo">查询</el-button>
        <el-button size="mini" icon="el-icon-search" @click="clearSearchInfo">重置</el-button>
        <el-button v-if="$route.query.type != 'add'" size="mini" type="danger" icon="el-icon-delete" @click="deleteInfoList">删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      style="width: 100%"
      border
      @selection-change="handleSelectionChange">
      <el-table-column
        v-if="$route.query.type != 'add'"
        align="center"
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        v-if="$route.query.type == 'add'"
        align="center"
        type="index"
        width="55">
      </el-table-column>
      <el-table-column
        align="center"
        prop="ep_name"
        label="考卷标题"
        minWidth="150">
      </el-table-column>
      <el-table-column
        align="center"
        prop="ep_describe"
        label="考卷描述"
        minWidth="250">
      </el-table-column>
      <el-table-column
        align="center"
        prop="ep_test_time"
        label="考试时间(分钟)"
        minWidth="100">
      </el-table-column>
      <el-table-column
        align="center"
        prop="ep_fraction"
        label="总分"
        minWidth="100">
      </el-table-column>
      <el-table-column
        align="center"
        prop="ep_time"
        label="创建时间"
        minWidth="120">
      </el-table-column>
      <el-table-column
        v-if="$route.query.type != 'add'"
        label="操作"
        minWidth="180"
        align="center">
        <template slot-scope="scope">
          <el-button size="mini"  type="success" @click="lookTestQuestion(scope.$index, scope.row)">查看考卷</el-button>
          <el-button size="mini"  @click="editInfo(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete"  size="mini" @click="deleteInfo(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column
        v-if="$route.query.type == 'add'"
        label="操作"
        minWidth="180"
        align="center">
        <template slot-scope="scope">
          <el-button size="mini"  type="success" @click="addTestRoomPaper(scope.$index, scope.row)">下发</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageInfo.page"
      :page-sizes="[10, 20, 30, 40,50]"
      :page-size="pageInfo.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.total">
    </el-pagination>
    <edit-examination-paper v-if="isShowUpdateDialog" :isShowUpdateDialog="isShowUpdateDialog" :examinationPaper="examinationPaper" @closeDialog="closeDialog"></edit-examination-paper>
    <edit-room-paper v-if="isShowRoomPaperDialog" :isShowRoomPaperDialog="isShowRoomPaperDialog" :roomPaperInfo="roomPaperInfo" @closeDialog="closeDialog"></edit-room-paper>
  </div>
</template>
<script>
  import {getAllSubject} from '@/api/questions'
  import {getExaminationPaper,getExaminationPaperById,updateExaminationById,deleteExaminationById} from '@/api/invigilate'
  import editExaminationPaper from './component/editExaminationPaper.vue'
  import editRoomPaper from '../examinationRoom/component/editRoomPaper.vue';
  import ElFormItem from "../../../../node_modules/element-ui/packages/form/src/form-item";
  export default{
    components:{
      ElFormItem,
      editExaminationPaper,
      editRoomPaper
    },
    created(){
        this.c_getAllSubject();
        this.getTableData();
        console.log(this.$route.query);
    },
    data(){
      return {
        tableData:[],
        selectList:[],

        isShowUpdateDialog:false,
        isShowRoomPaperDialog:false,

        examinationPaper:null,
        options:[], //选择所属科目

        roomPaperInfo:{},

        DataForm:{
          f_sb_id:0
        },
        pageInfo:{
          page:1,//页数
          size:10, //页面大小
          total:0 //页总数
        },
        requestData:{
          pageSize:10,
          pageNum:1,
          map:{
            ep_name:"",
            sb_id:0
          }
        }
      }
    },
    methods:{
      getTableData(){
        getExaminationPaper(this.requestData).then(res=>{
          if(res.code == 200){
              this.tableData = res.data.data;
              this.pageInfo.total = res.data.total;
          }
        })
      },

      //条件查询
      searchInfo(){
         if(this.DataForm.f_sb_id != 0){
           this.requestData.map.sb_id = parseInt(this.DataForm.f_sb_id[2]);  //课目
         }
         this.getTableData();
      },

      //重置
      clearSearchInfo(){
        this.DataForm.f_sb_id = 0;
        this.requestData.map.sb_id = 0;
        this.requestData.map.ep_name = "";
        this.getTableData();
      },
      //编辑
      editInfo(index,row){
          this.examinationPaper = row;
          this.isShowUpdateDialog = true;
      },

      //批量删除
      deleteInfoList(){
        if(this.selectList == null || this.selectList.length <= 0){
          this.$message({
            type: 'warning',
            message: '请至少选择一行要删除的数据'
          });
        }else{
          this.$confirm('此操作将删除数据, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            deleteExaminationById(this.selectList).then(res=>{
              if(res.code == 200){
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
                this.selectList = [];
                this.getTableData();
              }
            })
          }).catch(() => {

          });
        }
      },

      //删除
      deleteInfo(index,row){
        this.selectList = [row];
        this.deleteInfoList();
      },

      //查看考卷
      lookTestQuestion(index,row){
        this.$router.push({ path: '/invigilate/examinationPaper/component/lookExaminationPaper',query:{ep_id:row.ep_id,type:"vLook"}});
      },

      //复选框选择的数据
      handleSelectionChange(val){
        this.selectList = val;
      },

      closeDialog(type){
          if(type == "update"){
              this.isShowUpdateDialog = false;
              this.getTableData();
          }else if(type == "addRoomPaper"){
              this.isShowRoomPaperDialog = false;
              this.$router.push({ path: '/invigilate/examinationRoom/component/insertRoom/'+this.$route.query.id });
          }else if(type == "closeRoomPaper"){
            this.isShowRoomPaperDialog = false;
            this.getTableData();
          }
      },

      //返回对应考场
      returnRoom(){
        this.$router.push({ path: '/invigilate/examinationRoom/component/insertRoom/'+this.$route.query.id });
      },

      //下发试卷
      addTestRoomPaper(index,row){
          this.roomPaperInfo.tr_id = this.$route.query.id;
          this.roomPaperInfo.ep_id = row.ep_id;
          this.roomPaperInfo.type="add";
          this.isShowRoomPaperDialog = true;
      },

      //每页的条数发生改变时调用
      handleSizeChange(size){
        this.requestData.pageSize = size;
        this.pageInfo.size = size;
        this.getTableData();
      },

      //页数发生改变时调用
      handleCurrentChange(page){
        this.requestData.pageNum = page;
        this.pageInfo.page = page;
        this.getTableData();
      },

      c_getAllSubject(){
        getAllSubject().then(res=>{
          if(res.code == 200){
            for(let i = 0;i < res.data.length;i ++){
              let tLevel = {value:res.data[i].l_id,label:res.data[i].l_name};
              this.options.push(tLevel);
              if(res.data[i].list != null && res.data[i].list.length > 0){
                let gList = res.data[i].list;
                let children = [];
                for(let j = 0;j < gList.length;j ++){
                  let gGroup = {value:gList[j].g_id,label:gList[j].g_name};
                  children.push(gGroup);

                  if(gList[j].list != null && gList[j].list.length > 0){
                    let sList = gList[j].list;
                    let children1 = [];

                    for(let n = 0;n < sList.length;n ++){
                      let subject = {value:sList[n].sb_id,label:sList[n].sb_name};
                      children1.push(subject);
                    }
                    gGroup.children = children1;
                  }
                }
                tLevel.children = children;
              }
            }
          }
        });
      },
    }
  }
</script>
